/*
 * $Id: UtilizationLog.java,v 1.1 2009/10/06 19:05:18 navdeep_mahajan Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the
 * General Electric Company (GE). You shall not disclose this software and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.util;

import com.ge.med.service.eds.utilization.client.UtilizationData;
import com.ge.med.service.eds.utilization.client.UtilizationException;
import com.ge.med.service.eds.utilization.client.UtilizationObject;

/**
 * Purpose:
 * <ul>
 * <li>Used for logging to the EDS Utilization log</li>
 * </ul>
 * @author Navdeep Mahajan
 * @copyright (C) 2006 The General Electric Company
 */
public class UtilizationLog {
	// Initialize Application Name
	private String applicationName = null;
	private String featureName = null;
	private String userId = null;
	private String userRole = null;
	private String globalSessionID = null;
	static UtilizationObject utilObj = new UtilizationObject();

	/**
	 * Initializes the required attributes
	 * @param featureName
	 * @param userId
	 * @param userRole
	 */
	public UtilizationLog(String featureName, String userId, String userRole,
			String applicationName) {
		this.featureName = featureName;
		this.userId = userId;
		this.userRole = userRole;
		this.applicationName = applicationName;
	}

	/**
	 * Initializes the required attributes
	 * @param featureName
	 * @param userId
	 * @param userRole
	 * @throws UtilizationException 
	 */
	public UtilizationLog(String featureName, String userId, String userRole,
			String applicationName, boolean globalSession)
			throws UtilizationException {
		this.featureName = featureName;
		this.userId = userId;
		this.userRole = userRole;
		this.applicationName = applicationName;
		if (globalSession) {
			this.globalSessionID = UtilizationLog.getUniqueId();
		}
	}

	/**
	 * @param sessionId
	 * @param systemId
	 * @param logFileName
	 * @param eventName
	 * @param status
	 * @throws UtilizationException 
	 */
	public void log(String sessionId, String systemId, String logFileName,
			String eventName, String status) throws UtilizationException {
		Object[] dataElemArray = new Object[] { systemId, logFileName, status };
		// Initialize the UtilizationData Object through the appropriate
		// constructor
		UtilizationData utilData = new UtilizationData(applicationName,
				featureName, userId, userRole, eventName, dataElemArray);
		// Initialize the UtilizationObject Object through the default
		// constructor This constructor assumes the EDS_Utilization.properties
		// file in the base directory to set the application.name,
		// enable.logging and dataflush.period properties.
		utilData.setSessionId(sessionId);
		try {
			// Insert the UtilizationData object to the Utilization metrics
			// database.
			utilObj.insertObject(utilData);
		} catch (UtilizationException utilizationException) {
			throw utilizationException;
		}
	}

	/**
	 * @param systemId
	 * @param logFileName
	 * @param eventName
	 * @param status
	 * @throws UtilizationException 
	 */
	public void log(String systemId, String logFileName, String eventName,
			String status) throws UtilizationException {
		this.log(globalSessionID, systemId, logFileName, eventName, status);
	}

	/**
	 * Returns a unique session ID that is used for tracking
	 * the start and the end events
	 * @return - session id
	 * @throws Exception
	 */
	public static String getUniqueId() throws UtilizationException {
		String sessionID = "";
		sessionID = utilObj.getUniqueId();
		return sessionID;
	}
}
